package club.easin.sq.bl;

import club.easin.MYException;
import club.easin.common.DatagridRequestDto;
import club.easin.enums.Exenum;
import club.easin.enums.PwFinal;
import club.easin.enums.PwRegex;
import club.easin.pow.dto.UserSession;
import club.easin.sq.dao.SqCheckMapper;
import club.easin.sq.dao.SqInmateMapper;
import club.easin.sq.dao.SqInmateTempMapper;
import club.easin.sq.dto.*;
import club.easin.utils.*;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("Inmatebl")
public class Inmatebl {

    @Autowired
    private SqInmateMapper sqInmateMapper;
    @Autowired
    private SqCheckMapper sqCheckMapper;
    @Autowired
    private SqInmateTempMapper sqInmateTempMapper;
    @Autowired
    private DBSubmit dbSubmit;

    public ReturnDto getInmateGrild(DatagridRequestDto<InmateQueryDto> dto){
        Page<InmateDatile> page=null;
        PageHelper.startPage(dto.getPagenum(),dto.getPagesize());
        InmateQueryDto queryDto=dto.getData();
        page=sqInmateMapper.getInmateGrildList(queryDto);
        return ReturnUtils.returnsuccess(DataGrildUtils.returnDataGrild(page));
    }

    public ReturnDto addInmate(UserSession userSession, SqInmateTemp sqInmateTemp){
        String checkid=Commutils.buildId();
        Date now=DateUtils.getNow();
        SqCheck sqCheck=new SqCheck(checkid,PwFinal.nocheck,null,null,now,null);
        sqCheck.setType(PwFinal.check_insert);
        String error=checkInmate(sqInmateTemp,true);
        if(!CheckUtils.isNull(error)){
            return ReturnUtils.returnError(error);
        }
        addcheckInmate(sqCheck,userSession,sqInmateTemp);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto inmateCheck(UserSession userSession, CheckDto checkDto){
        Tlist tlist=new Tlist();
        SqInmateTemp sqInmateTemp=sqInmateTempMapper.selectByPrimaryKey(checkDto.getTempid());
        if(sqInmateTemp!=null){
            String checkid=sqInmateTemp.getCheckid();
            SqCheck check=sqCheckMapper.selectByPrimaryKey(checkid);
            check.setCheckuser(userSession.getUser().getUserid());
            if(!check.getState().equals(PwFinal.nocheck)) throw new MYException(Exenum.IS_CHECK);
          if(checkDto.getState().equals(PwFinal.checkpass)){
            String sqtemp=JSON.toJSONString(sqInmateTemp);
            SqInmate sqInmate=JSON.parseObject(sqtemp,SqInmate.class);
            sqInmate.setInmateid(Commutils.buildId());
            sqInmate.setVaildflag(PwFinal.isvaild);
            tlist.add(SqInmateMapper.class,"insert",sqInmate);
            check.setState(PwFinal.checkpass);
            check.setChecktime(DateUtils.getNow());
            tlist.add(SqCheckMapper.class,"updateByPrimaryKeySelective",check);
          }else{
              check.setState(PwFinal.checkpass);

              tlist.add(SqCheckMapper.class,"updateByPrimaryKeySelective",check);
          }

        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto updateInmateCheck(UserSession userSession,CheckDto checkDto){
        Tlist tlist=new Tlist();
        SqInmateTemp sqInmateTemp=sqInmateTempMapper.selectByPrimaryKey(checkDto.getTempid());
        if(sqInmateTemp!=null){
            String checkid=sqInmateTemp.getCheckid();
            SqCheck check=sqCheckMapper.selectByPrimaryKey(checkid);
            check.setCheckuser(userSession.getUser().getUserid());
            if(!check.getState().equals(PwFinal.nocheck)) throw new MYException(Exenum.IS_CHECK);
            if(CheckUtils.isNull(sqInmateTemp.getInmateid())) return ReturnUtils.returnsuccess();
            if(PwFinal.checkpass.equals(checkDto.getState())){
                String sqtemp=JSON.toJSONString(sqInmateTemp);
                SqInmate sqInmate=JSON.parseObject(sqtemp,SqInmate.class);
                tlist.add(SqInmateMapper.class,"updateByPrimaryKey",sqInmate);
                check.setState(PwFinal.checkpass);
                check.setChecktime(DateUtils.getNow());
                tlist.add(SqCheckMapper.class,"updateByPrimaryKeySelective",check);
            }else{
                check.setState(PwFinal.checkpass);
                tlist.add(SqCheckMapper.class,"updateByPrimaryKeySelective",check);
            }

        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto editInmate(UserSession userSession,SqInmateTemp sqInmateTemp){
        String checkid=Commutils.buildId();
        Date now=DateUtils.getNow();
        SqCheck sqCheck=new SqCheck(checkid,PwFinal.nocheck,null,null,now,null);
        sqCheck.setType(PwFinal.check_update);
        String error=checkInmate(sqInmateTemp,true);
        if(!CheckUtils.isNull(error)){
            return ReturnUtils.returnError(error);
        }
        addcheckInmate(sqCheck,userSession,sqInmateTemp);
        return ReturnUtils.returnsuccess();
    }

    public void addcheckInmate(SqCheck sqCheck,UserSession userSession,SqInmateTemp sqInmateTemp){
        Tlist tlist=new Tlist();
        Date now=DateUtils.getNow();
        sqInmateTemp.setTempid(Commutils.buildId());
        sqInmateTemp.setCreatetime(now);
        sqInmateTemp.setUpdatetime(now);
        sqInmateTemp.setCheckid(sqCheck.getCheckid());
        tlist.add(SqInmateTempMapper.class,"insert",sqInmateTemp);
        tlist.add(SqCheckMapper.class,"insert",sqCheck);
        dbSubmit.exce(tlist);
    }

    public ReturnDto getInmateTempGrild(DatagridRequestDto<InmateQueryDto> dto){
        Page<InmateTempDatile> page=null;
        PageHelper.startPage(dto.getPagenum(),dto.getPagesize());
        InmateQueryDto queryDto=dto.getData();
        page=sqInmateTempMapper.getInmateTemgGrild(queryDto);
        return ReturnUtils.returnsuccess(DataGrildUtils.returnDataGrild(page));
     }

    public ReturnDto getInmateBykey(String inmateid){
       SqInmate sqInmate=sqInmateMapper.selectByPrimaryKey(inmateid);
       return ReturnUtils.returnsuccess(sqInmate);
    }

    public ReturnDto getInmateTempByid(String tempid){
        SqInmateTemp sqInmateTemp=sqInmateTempMapper.selectByPrimaryKey(tempid);
        return ReturnUtils.returnsuccess(sqInmateTemp);
    }

    public ReturnDto getInmateByidcode(String idcode){
        List<SqInmate> sqInmates=sqInmateMapper.getSqInamteByidcode(Commutils.encod(idcode));
        return ReturnUtils.returnsuccess(sqInmates);
    }

    public String checkInmate(SqInmateTemp sqInmateTemp,boolean isAdd){
        StringBuffer error=new StringBuffer();
        if(isAdd){
            if(sqInmateTemp.getEmaile()==null){
                error.append("邮箱不能为空,");
            }
            if(!sqInmateTemp.getEmaile().getValue().matches(PwRegex.emailRegx)){
                error.append("邮箱格式不正确,");
            }
            if(sqInmateTemp.getTelephone()==null){
                error.append("手机号不能为空,");
            }
            if(!sqInmateTemp.getTelephone().getValue().matches(PwRegex.phoneRegex)){
                error.append("手机号格式不正确,");
            }
            if(sqInmateTemp.getIdcode()==null){
                error.append("身份证号不能为空,");
            }
            if(!sqInmateTemp.getIdcode().getValue().matches(PwRegex.idcodeRegx)){
                error.append("身份证格式不正确,");
            }
        }else{
            if(sqInmateTemp.getEmaile()!=null){
                if(!sqInmateTemp.getEmaile().getValue().matches(PwRegex.emailRegx)){
                    error.append("邮箱格式不正确,");
                }
            }
            if(sqInmateTemp.getTelephone()!=null){
                if(!sqInmateTemp.getTelephone().getValue().matches(PwRegex.phoneRegex)){
                    error.append("手机号格式不正确,");
                }
            }

            if(sqInmateTemp.getIdcode()!=null){
                if(!sqInmateTemp.getIdcode().getValue().matches(PwRegex.idcodeRegx)){
                    error.append("身份证格式不正确,");
                }
            }

        }
        return error.toString();
    }



}
